package com.wmx.controller;

import com.wmx.entity.TV;
import com.wmx.service.TVService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.security.SecureRandom;
import java.util.Date;
import java.util.List;

/**
 * 电视控制层——演示 Spring Boot @Transactional 配置事务管理
 *
 * @author wangMaoXiong
 * @version 1.0
 * @date 2022/4/9 8:33
 */
@Controller
public class TVController {

    @Resource
    private TVService tvService;

    /**
     * http://localhost:8080/tv/findAll
     *
     * @return
     */
    @GetMapping("/tv/findAll")
    @ResponseBody
    public List<TV> findAll() {
        return tvService.findAll();
    }

    /**
     * http://localhost:8080/tv/save
     *
     * @return
     */
    @GetMapping("/tv/save")
    public String save() {
        TV tv = new TV();
        tv.setTvName("海信RS" + new SecureRandom().nextInt(1000));
        tv.setTvPrice(new SecureRandom().nextFloat() * 1000 + 6000);
        tv.setDateOfProduction(new Date());
        tvService.save(tv);
        return "redirect:/tv/findAll";
    }

    /**
     * http://localhost:8080/tv/save2
     *
     * @return
     */
    @GetMapping("/tv/save2")
    public String save2() {
        TV tv = new TV();
        tv.setTvName("长虹T" + new SecureRandom().nextInt(1000));
        tv.setTvPrice(new SecureRandom().nextFloat() * 1000 + 6000);
        tv.setDateOfProduction(new Date());
        tvService.save2(tv);
        return "redirect:/tv/findAll";
    }

    /**
     * http://localhost:8080/tv/deleteById2?id=23
     *
     * @param id
     * @return
     */
    @GetMapping("/tv/deleteById2")
    public String deleteById2(int id) {
        tvService.deleteById2(id);
        return "redirect:/tv/findAll";
    }

}
